﻿using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.UI;

public class PointerManager : MonoBehaviour
{
    public Transform player1;
    public Transform player2;
    public Camera camera;
    public Transform leftPointer;
    public Transform rightPointer;
    private Vector3 diff;
    private int f = 0;

    // Start is called before the first frame update
    [System.Obsolete]
    void Start()
    {
        diff = player1.position - player2.position;
        foreach (Transform child in leftPointer)
        {
            child.GetComponent<UnityEngine.UI.Image>().enabled = false;
        }
        foreach (Transform child in rightPointer)
        {
            child.GetComponent<UnityEngine.UI.Image>().enabled = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 flag = player1.position - player2.position;

        if(flag.y < 0 && f ==0)
        {
            diff = -diff;
            f = 1;
        }
        if(flag.y>0 && f == 1)
        {
            diff = -diff;
            f = 0;
        }
        foreach (Transform child in leftPointer)
        {
            child.GetComponent<UnityEngine.UI.Image>().enabled = false;
        }
        foreach (Transform child in rightPointer)
        {
            child.GetComponent<UnityEngine.UI.Image>().enabled = false;
        }
        float orthographicSize = camera.orthographicSize;
        Vector3 player2in1 = player2.position + diff;
        Vector3 Direction = player1.position - player2in1;
        float distance = Direction.sqrMagnitude;
        if(distance > orthographicSize*1.44) {
            if (Direction.x > orthographicSize/2 && Direction.y > orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("NorthEast");
                Transform lpointer = leftPointer.Find("SouthWest");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("NorthEast!");
            }
            if (Direction.x > orthographicSize / 2 && Direction.y < -orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("SouthEast");
                Transform lpointer = leftPointer.Find("NorthWest");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("SouthEast!");
            }
            if (Direction.x < -orthographicSize / 2 && Direction.y < -orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("SouthWest");
                Transform lpointer = leftPointer.Find("NorthEast");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("SouthWest!");
            }
            if (Direction.x < -orthographicSize / 2 && Direction.y > orthographicSize / 2)
            { 
                Transform rpointer = rightPointer.Find("NorthWest");
                Transform lpointer = leftPointer.Find("SouthEast");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("NorthWest!");
            }
            if (Direction.x <= orthographicSize / 2 && Direction.x >= -orthographicSize / 2 && Direction.y > orthographicSize / 2)
            {
                   Transform rpointer = rightPointer.Find("North");
                    Transform lpointer = leftPointer.Find("South");
                    lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                    rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                    Debug.Log("North!");
            }
            if (Direction.x <= orthographicSize / 2 && Direction.x >= -orthographicSize / 2 && Direction.y < -orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("South");
                Transform lpointer = leftPointer.Find("North");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("South!");
            }
            if (Direction.x > orthographicSize / 2 && Direction.y <= orthographicSize / 2 && Direction.y >= -orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("East");
                Transform lpointer = leftPointer.Find("West");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("East!");
            }
            if (Direction.x < -orthographicSize / 2 && Direction.y <= orthographicSize / 2 && Direction.y >= -orthographicSize / 2)
            {
                Transform rpointer = rightPointer.Find("West");
                Transform lpointer = leftPointer.Find("East");
                lpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                rpointer.GetComponent<UnityEngine.UI.Image>().enabled = true;
                Debug.Log("West!");
            }
        }
        
        

    }
}
